package com.supermap.dataservice.disaster.dao;

import com.supermap.dataservice.util.Table;
import com.supermap.dubbo.api.disaster.entity.DisasterAllInfo;
import com.supermap.dubbo.api.disaster.entity.DisasterComment;
import com.supermap.dubbo.api.disaster.entity.DisasterType;
import com.supermap.dubbo.api.disaster.entity.Disasterpraise;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author lxs
 * @Title:灾害数据访问层
 * @Description:
 * @date 2018/3/1  15:15
 */
@Mapper
public interface DisasterTypeDao {

    @Select("select areaCode,warnAreaCode from t_area_warn where areaCode like concat(#{areaCode},'%') ")
    List<Map<String,Object>> getAllWarnArea(@Param("areaCode") String areaCode);

    @Select("select areaCode,warnAreaCode from t_area_warn")
    List<Map<String,Object>> getAllArea();


    /**
     * 通过等级字段获得所有的灾情信息
     * @return
     */
    @Select("select * from t_disastertype where level=#{level}")
    public List<DisasterType> getAllDisasterTypeByLevels(@Param("level") int level);
    /**所有的灾害信息
     * 查询
     * @return
     */
    @Select("select * from t_disastertype ")
    List<DisasterType> getAllDisasterType();

    /**
     * 根据灾情ID得到所有的点赞数
     * @param disasterId
     * @return
     */
    @Select("select COUNT(*) from t_disasterpraise where disasterId=#{disasterId}")
    int getDisasterPraiseCount(@Param("disasterId") int disasterId);

    /**
     * 根据灾情ID和userId判断该灾情是否被该user点过赞
     * @param userId
     * @param disasterId
     * @return
     */
    @Select("select isPraise from   t_disasterpraise where userId=#{userId} and disasterId=#{disasterId}")
    String  getDisasterPraise(@Param("userId") int userId, @Param("disasterId") int disasterId);

    /**
     *添加点赞
     * @param disasterpraise
     * @return
     */
    @Insert("Insert into t_disasterpraise(disasterId,isPraise,userId) "+"values(#{disasterpraise.disasterId},#{disasterpraise.isPraise},#{disasterpraise.userId}"+")")
    int addDisasterPraise(@Param("disasterpraise") Disasterpraise disasterpraise);

    @Delete("delete from t_disasterpraise where  userId=#{userId} and disasterId=#{disasterId}")
    int DelDisById(@Param("userId") int userId, @Param("disasterId") int disasterId);

    /**
     * 根据灾害ID 得到对应下的所有评论
     * @param disasterId
     * @return
     */
    @Select("select t1.*,t2.username from t_disastercomment t1 inner JOIN t_user t2 on t1.userId=t2.id  where t1.disasterId=#{disasterId}")
    List<DisasterComment> getAllDisasterCommentByagrInfoId(@Param("disasterId") int disasterId);

    /**
     * 添加灾害评论
     * @param disasterComment
     * @return
     */
    @Insert("Insert into t_disastercomment(disasterId,comment,parentId,userId) "+"values(#{disasterComment.disasterId},#{disasterComment.comment},#{disasterComment.parentId},#{disasterComment.userId}"+")")
    int addDisasterComment(@Param("disasterComment") DisasterComment disasterComment);

    /**
     * 添加 默认为未阅读的评论数据
     * @param userid
     * @param typeid
     * @param type
     * @param comment
     * @param state
     * @return
     */
    @Insert("Insert into t_newmessage(userid,typeid,type,comment,state,uid) "
            +"values(#{userid},#{typeid},#{type},#{comment},#{state},#{uid})")
    int addNewMessage(@Param("userid") int userid, @Param("typeid") int typeid, @Param("type") int type, @Param("comment") String comment, @Param("state") int state, @Param("uid") String uid);

    /**
     * 根据userId获取该灾情的评论及相关信息 分页
     * @param table
     * @param userId
     * @return
     */
    @Select("select t1.id,t1.`longitude` AS lon,t1.`latitude` AS lat,t1.descript,t2.username as publisherName,t2.address as publisherAddress,t2.HeadImage as publisherHeadImage,t1.img_url as publisherAgrImg ,t1.createtime as publisherTime ,t1.development as development,t3.`name` AS cropName,t4.name AS disName " +
            "from  t_disaster t1 INNER JOIN t_user t2 on t1.userId=t2.id INNER JOIN t_crop t3 ON t1.`type`=t3.`id` INNER JOIN t_disastertype t4 ON t1.`disastertype`=t4.`code` " +
            " where t1.userId=#{userId} and t1.del_flg=0 ORDER BY t1.createtime desc limit #{table.startPoint},#{table.limit}")
    List<DisasterAllInfo> getDisAllInfoByUserId(@Param("table") Table table, @Param("userId")String userId);
    @Select("select count(*) from  t_disaster t1 INNER JOIN t_user t2 on t1.userId=t2.id INNER JOIN t_crop t3 ON t1.`type`=t3.`id` INNER JOIN t_disastertype t4 ON t1.`disastertype`=t4.`code` " +
            " where t1.userId=#{userId} and t1.del_flg=0 ORDER BY t1.createtime desc")
    int getDisCountByUserId(@Param("userId")String userId);

    /**
     * 根据区域code查询所有灾情的评论及相关信息 分页
     * @param table
     * @param areaCode
     * @return
     */
    @Select("select t1.id,t1.`longitude` AS lon,t1.`latitude` AS lat,t1.descript,t2.username as publisherName,t2.address as publisherAddress,t2.HeadImage as publisherHeadImage,t1.img_url as publisherAgrImg ,t1.createtime as publisherTime,t1.development as development,t3.`name` AS cropName,t4.name AS disName \n" +
            "from  t_disaster t1 INNER JOIN t_user t2 on t1.userId=t2.id  INNER JOIN t_crop t3 ON t1.`type`=t3.`id` INNER JOIN t_disastertype t4 ON t1.`disastertype`=t4.`code` " +
            " where t1.areacode like #{areaCode} and t1.del_flg=0 ORDER BY t1.createtime desc limit #{table.startPoint},#{table.limit}")
    List<DisasterAllInfo> getDisAllInfoByAreaCode(@Param("table") Table table, @Param("areaCode")String areaCode);

    /**
     * 根据areaCode查询灾情总数
     * @param areaCode
     * @return
     */
    @Select("select count(*) from  t_disaster t1 INNER JOIN t_user t2 on t1.userId=t2.id  INNER JOIN t_crop t3 ON t1.`type`=t3.`id` INNER JOIN t_disastertype t4 ON t1.`disastertype`=t4.`code` " +
            " where  t1.areacode like #{areaCode} and t1.del_flg=0 ORDER BY t1.createtime desc")
    int getDisCountByAreaCode(@Param("areaCode")String areaCode);

    /**
     * 根据区域编码获取对应的预警区域编码
     * @param areaCode
     * @return
     */
    @Select("SELECT warnAreaCode FROM t_area_warn where areaCode like #{areaCode}")
    List<String> getWarnAreaCode(@Param("areaCode")String areaCode);


}
